package com.zjgeport.zjgdanger.OnPortDynamics;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

import com.github.abel533.echarts.Title;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.code.X;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import com.soar.PageInfo;
import com.soar.util.net.rmi.httpxml.HttpCallBack;
import com.soar.util.net.rmi.httpxml.HttpClient;
import com.soar.util.net.rmi.httpxml.HttpMethod;
import com.soar.util.net.rmi.httpxml.HttpResult;
import com.zjgeport.zjgdanger.CommonActivity;
import com.zjgeport.zjgdanger.LoginActivity;
import com.zjgeport.zjgdanger.R;
import com.zjgeport.zjgdanger.common.LogUtils;
import com.zjgeport.zjgdanger.common.ParamCommon;
import com.zjgeport.zjgdanger.model.myApplication;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class DangerCountActivity extends CommonActivity {

    private WebView mWebView;
    ProgressDialog dialog = null;

    /** Handler What加载数据完毕 **/
    private static final int WHAT_DID_LOAD_DATA = 0;

    /** Handler What更新数据完毕 **/
    private static final int WHAT_DID_REFRESH = 1;

    /** Handler What更多数据完毕 **/
    private static final int WHAT_DID_MORE = 2;

    /** 分页信息 **/
    protected PageInfo pageInfo = new PageInfo();

    private String  sum1,sum2,sum3,sum4,sum5,sum6,sum7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_danger_count);
        initCommonListener();
        initView();
    }

    private void initView()
    {
        sum1 = getIntent().getStringExtra("SUM1");
        sum2 = getIntent().getStringExtra("SUM2");
        sum3 = getIntent().getStringExtra("SUM3");
        sum4 = getIntent().getStringExtra("SUM4");
        sum5 = getIntent().getStringExtra("SUM5");
        sum6 = getIntent().getStringExtra("SUM6");
        sum7 = getIntent().getStringExtra("SUM7");

        findTextViewById(R.id.title).setText("危险货物趋势图");
        dialog = new ProgressDialog(this);
        dialog.setMessage("正在查询...");

        mWebView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setSupportZoom(true);
        webSettings.setDisplayZoomControls(true);

        mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

        mWebView.loadUrl("file:///android_asset/jsWeb/echart.html");

    }

    private class MyHandler extends Handler
    {
        public MyHandler(Looper looper)
        {
            super(looper);
        }

        @SuppressWarnings("unchecked")
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case WHAT_DID_LOAD_DATA:



                    break;


                default:
                    break;
            }
        }
    }

    private void showDialog(){
        dialog.show();
    }

    private void dismissDialog(){
        dialog.dismiss();
    }


    /**
     * 注入到JS里的对象接口
     */
    class WebAppInterface {
        Context mContext;

        public WebAppInterface(Context c) {
            mContext = c;
        }

        /**
         * 获取
         *
         * @return
         */
        @JavascriptInterface
        public String getLineChartOptions() {
            GsonOption option = markLineChartOptions();
            LogUtils.d(option.toString());
            return option.toString();
        }

        @JavascriptInterface
        public void showToast(String msg) {
            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
        }

        /**
         * PieChart 或者柱状图数据示例
         * @param type 1 - 饼状图数据； 2 - 柱状图数据
         * @return
         */
        @JavascriptInterface
        public String getPieChartOptions(int type) {

            mWebView.post(new Runnable() {
                @Override
                public void run() {
                    showDialog();
                }
            });
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //地址：http://echarts.baidu.com/doc/example/pie7.html
            GsonOption option = new GsonOption();
            // 设置标题
            option.title(new Title().text("某站点用户访问来源").subtext("纯属虚构").x(X.center));
            if(type == 2) {
                option.tooltip().trigger(Trigger.item).formatter("{a} <br/>{b} : {c} ({d}%)");
            }else  if(type == 1){
                option.tooltip().trigger(Trigger.item).formatter("{a} <br/>{b} : {c}");
            }
            // 设置图例
            option.legend().data("直接访问", "邮件营销", "联盟广告", "视频广告", "搜索引擎").orient(Orient.vertical).x(X.left);

            // 是否可以拖动以计算
            option.calculable(true);


            Log.d("TAG",option.toString());
            mWebView.post(new Runnable() {
                @Override
                public void run() {
                    dismissDialog();
                }
            });
            return option.toString();
        }


        @JavascriptInterface
        public GsonOption markLineChartOptions() {
            GsonOption option = new GsonOption();
            option.title("危险货物趋势图","单位:万吨");

            option.toolbox().show(true);
            option.tooltip().trigger(Trigger.axis).formatter("{b}总量：{c}万吨");

            ValueAxis valueAxis = new ValueAxis();
            valueAxis.axisLabel().formatter("{value}万吨");
            option.yAxis(valueAxis);

            CategoryAxis categoryAxis = new CategoryAxis();
            categoryAxis.axisLabel().formatter("{value}");
            categoryAxis.data(getPastDate(6),getPastDate(5),getPastDate(4),getPastDate(3),getPastDate(2),getPastDate(1),getPastDate(0));
            option.xAxis(categoryAxis);

            Line line = new Line();
            line.data(sum7, sum6, sum5, sum4, sum3, sum2, sum1);
            option.series(line);
            return option;
        }
    }

    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        String result = format.format(today);
        Log.e(null, result);
        return result;
    }
}
